<?php
/**
 * 检查用户是否登录
 */
function check_login() 
{
    write_log("session:" . json_encode($_SESSION));
    if (empty($_SESSION["user"]))
    {
        return false;
    }
    return true;
}

function getSessionUser() 
{
    return $_SESSION['user_info']['user_name'];
}

function write_log($str, $level = 'DEBUG'){
    error_log(date('[Y-m-d H:i:s]', time()) . $str . "\n", 3, "/tmp/error.log");
}

/**
 * 获取GET参数
 * @param string &$param 存储变量
 * @param string $name 字段名
 * @param string $default 默认值
 * @return boolean
 */
function g(&$param, $name, $default=null){
    return _request($param, $name, $default, 0);
}

/**
 * 获取POST参数
 * @param string &$param 存储变量
 * @param string $name 字段名
 * @param string $default 默认值
 * @return boolean
 */
function p(&$param, $name, $default=null) {
    return _request($param, $name, $default, 1);
}

/**
 * 获取GET/POST参数
 * @param string &$param 存储变量
 * @param string $name 字段名
 * @param string $default 默认值
 * @return boolean
 */
function b(&$param, $name, $default=null) {
    return _request($param, $name, $default, 2);
}

/**
 * 获取GET/POST参数
 * @param string &$param 存储变量
 * @param string $name 字段名
 * @param string $default 默认值
 * @param int $type 获取方式: 0-GET, 1-POST, 2-BOTH.
 * @return boolean
 */
function _request(&$param, $name, $default, $type) {
    $_param = null;
    if(0==$type && isset($_GET[$name])) {
        $_param = stripslashes(trim($_GET[$name]));
    }
    elseif(1==$type && isset($_POST[$name])){
        $_param = stripslashes(trim($_POST[$name]));
    }
    elseif(2 == $type){
        if(isset($_POST[$name])){
            $_param = stripslashes(trim($_POST[$name]));
        }
        elseif(isset($_GET[$name])){
            $_param = stripslashes(trim($_GET[$name]));
        }
    }

    if(is_null($_param) or $_param == ''){
        if(is_numeric($param)) {
            $param = $default + 0;
        } else {
            $param = $default . '';
        }

        return false;
    } else {
        if(is_numeric($param)) {
            if(!is_numeric($_param)) {
                $param = $default + 0;

                return false;
            }
        }

        $param = $_param;
        return true;
    }
}

class ErrorCode
{
    const SUCC = 0 ;
    const UNKNOWN_ERROR = -1;
    const NO_LOGIN = -2;
    const TIMEOUT = -3;
    const DB_ERROR = -4;
    const PARAM_INVALID = -5;
    const DICE_INVALID = -6;
    
}

function returnError($error, $errmsg, $action = "SYSTEM")
{
    write_log("return error:" . json_encode(array("action"=> $action, "code" => $error, "errmsg" => $errmsg)));
    echo(json_encode(array("action"=> $action, "code" => $error, "errmsg" => $errmsg)));
    return $error;
}
function returnSucc($action, $data, $error =0)
{
    write_log("return succ" . json_encode(array("action"=> $action, "code" => $error, "data" => $data)));
    echo(json_encode(array("action"=> $action, "code" => $error, "data" => $data)));
}
